﻿using System;
using System.Linq;
using System.Web.Services;
using System.Collections.Specialized;


namespace GPSTrackingWebServicesBL
{
    /// <summary>
    /// Summary description for GPSTrackingWSBL
    /// </summary>
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class GPSTrackingControllerWSBL : System.Web.Services.WebService
    {

        [WebMethod]
        public string wsInsertGeoFence(string imei, string description, StringCollection latitudePolygonoList, StringCollection longitudePolygonoList)
        {
            float latitude;
            float longitude;

            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFence> geoFenceList = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFence>();
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFenceDetails> geoFenceDetailsList = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFenceDetails>();

            GPSTrackingServerDB.Entities.GeoFence geoFenceEntity = new GPSTrackingServerDB.Entities.GeoFence();
            GPSTrackingServerDB.Entities.GeoFenceByImei geoFenceByImeiEntity = new GPSTrackingServerDB.Entities.GeoFenceByImei();
            GPSTrackingServerDB.Entities.GeoFenceDetails geoFenceDetailsEntity;


            GPSTrackingServerDB.Data.TransactionManager tm = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();

            try
            {
                tm.BeginTransaction();
                geoFenceEntity.Description = description;

                GPSTrackingServerDB.Data.GeoFenceQuery geoFenceCondition = new GPSTrackingServerDB.Data.GeoFenceQuery();
                geoFenceCondition.Append(GPSTrackingServerDB.Entities.GeoFenceColumn.Description, geoFenceEntity.Description);

                if (GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceByImeiProvider.ExistGeoFenceByImeiAndDescription(imei, description))
                    throw new Exception("El nombre definido para el Polígono ya esta registrado para este vehículo, debe cambiar el nombre del Polígono.");

                GPSTrackingServerDB.Data.DataRepository.GeoFenceProvider.Insert(tm, geoFenceEntity);

                geoFenceByImeiEntity.Imei = imei;
                geoFenceByImeiEntity.GeoFenceId = geoFenceEntity.GeoFenceId;

                GPSTrackingServerDB.Data.DataRepository.GeoFenceByImeiProvider.Insert(tm, geoFenceByImeiEntity);

                for (int i = 0; i < latitudePolygonoList.Count && i < longitudePolygonoList.Count; i++)
                {
                    geoFenceDetailsEntity = new GPSTrackingServerDB.Entities.GeoFenceDetails();

                    latitude = float.Parse(latitudePolygonoList[i]);
                    longitude = float.Parse(longitudePolygonoList[i]);

                    geoFenceDetailsEntity.GeoFenceId = geoFenceEntity.GeoFenceId;
                    geoFenceDetailsEntity.NumPos = i + 1;
                    geoFenceDetailsEntity.Latitude = latitude;
                    geoFenceDetailsEntity.Longitude = longitude;

                    geoFenceDetailsList.Add(geoFenceDetailsEntity);
                }

                GPSTrackingServerDB.Data.DataRepository.GeoFenceDetailsProvider.Insert(tm, geoFenceDetailsList);

                tm.Commit();

                return GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceDetailsProvider.GetGeoFenceDetailsByGeoFenceID(geoFenceEntity.GeoFenceId);
            }
            catch (Exception e)
            {
                tm.Rollback();
                throw e;
            }
        }

        [WebMethod]
        public StringCollection wsLoadRouteHistory(string imei, DateTime date)
        {
            try
            {
                return GPSTrackingServerDBCustom.Data.DataRepository.TrackDataProvider.GetAllByImeiAndDate(imei, date);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [WebMethod]
        public string wsUpdateGeoFenceByGeoFenceID(string username, int geoFenceID, string description, string imei, StringCollection latitudePolygonoList, StringCollection longitudePolygonoList)
        {

            float latitude;
            float longitude;
            GPSTrackingServerDB.Data.TransactionManager tm = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFenceDetails> geoFenceDetailsEntityList;
            GPSTrackingServerDB.Entities.GeoFenceDetails geoFenceDetailsEntity;
            GPSTrackingServerDB.Entities.GeoFence geoFenceEntity;

            try
            {
                tm.BeginTransaction();

                geoFenceEntity = new GPSTrackingServerDB.Entities.GeoFence();

                geoFenceEntity.GeoFenceId = geoFenceID;
                geoFenceEntity.Description = description;

                if (description != "")
                {
                    if (GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceByImeiProvider.ExistGeoFenceByImeiAndDescription(imei, description))
                        throw new Exception("El nombre definido para el Polígono ya esta registrado para este vehículo, debe cambiar el nombre del Polígono o dejar el nombre en blanco para poder actualizar el polígono.");

                    GPSTrackingServerDB.Data.DataRepository.GeoFenceProvider.Update(tm, geoFenceEntity);
                }

                geoFenceDetailsEntityList = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.GeoFenceDetails>();

                geoFenceDetailsEntityList = GPSTrackingServerDB.Data.DataRepository.GeoFenceDetailsProvider.GetByGeoFenceId(tm, geoFenceID);

                GPSTrackingServerDB.Data.DataRepository.GeoFenceDetailsProvider.Delete(tm, geoFenceDetailsEntityList);

                geoFenceDetailsEntityList.Clear();

                for (int i = 0; i < latitudePolygonoList.Count && i < longitudePolygonoList.Count; i++)
                {
                    geoFenceDetailsEntity = new GPSTrackingServerDB.Entities.GeoFenceDetails();

                    latitude = float.Parse(latitudePolygonoList[i]);
                    longitude = float.Parse(longitudePolygonoList[i]);

                    geoFenceDetailsEntity.GeoFenceId = geoFenceID;
                    geoFenceDetailsEntity.NumPos = i + 1;
                    geoFenceDetailsEntity.Latitude = latitude;
                    geoFenceDetailsEntity.Longitude = longitude;

                    geoFenceDetailsEntityList.Add(geoFenceDetailsEntity);
                }

                GPSTrackingServerDB.Data.DataRepository.GeoFenceDetailsProvider.Insert(tm, geoFenceDetailsEntityList);
                tm.Commit();

                return GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceDetailsProvider.GetGeoFenceDetailsByGeoFenceID(geoFenceID);
            }
            catch (Exception e)
            {
                tm.Rollback();
                throw e;
            }
        }

        [WebMethod]
        public string wsGetGroupsTrackDataLastLocationByUser(string username)
        {
            try
            {
                return GPSTrackingServerDBCustom.Data.DataRepository.TrackDataLastLocationProvider.GetGroupsTrackDataLastLocationByUser(username);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [WebMethod]
        public string wsGetImeiTrackDataLastLocationByUser(string username)
        {
            try
            {
                return GPSTrackingServerDBCustom.Data.DataRepository.TrackDataLastLocationProvider.GetImeiTrackDataLastLocationByUser(username);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [WebMethod]
        public void wsDeleteGeoFence(int geoFenceID, string imei)
        {
            try
            {
                GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceProvider.DeleteGeoFenceCompletely(geoFenceID, imei);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [WebMethod]
        public string wsGetAllGeoFenceDetails(string user)
        {
            try
            {
                return GPSTrackingServerDBCustom.Data.DataRepository.GeoFenceDetailsProvider.GetAllGeoFenceDetailsByUser(user);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        [WebMethod]
        public string GetDate()
        {
            return DateTime.Now.ToShortDateString();
        }

    }
}